package com.ruoyi.web.service.impl;

import java.util.List;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AnonymousAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Service;
import com.ruoyi.web.mapper.CommentMapper;
import com.ruoyi.web.domain.Comment;
import com.ruoyi.web.service.ICommentService;

/**
 * commentService业务层处理
 *
 * @author ruoyi
 * @date 2024-04-22
 */
@Service
public class CommentServiceImpl implements ICommentService {
    @Autowired
    private CommentMapper commentMapper;
    @Autowired
    private ISysUserService userService;

    /**
     * 查询comment
     *
     * @param id comment主键
     * @return comment
     */
    @Override
    public Comment selectCommentById(Long id) {
        return commentMapper.selectCommentById(id);
    }

    /**
     * 查询comment列表
     *
     * @param comment comment
     * @return comment
     */
    @Override
    public List<Comment> selectCommentList(Comment comment) {
        return commentMapper.selectCommentList(comment);
    }

    /**
     * 新增comment
     *
     * @param comment comment
     * @return 结果
     */
    @Override
    public int insertComment(Comment comment) {
        // 手动设置userId、userName、avatar

        // 获取userId并赋值
        if (getUserId() != null) {
            comment.setUserId(getUserId());// userId
            SysUser sysUser = userService.selectUserById(getUserId());
            comment.setUsername(sysUser.getUserName());// username
            if (!StringUtils.isEmpty(sysUser.getAvatar())) {
                // 头像非空，赋值头像
                comment.setAvatar(sysUser.getAvatar());
            }
        }
        // 插入评论
        return commentMapper.insertComment(comment);
    }

    /**
     * 获取当前登录用户的userId
     */
    private Long getUserId() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        if (authentication != null && authentication.isAuthenticated() && !(authentication instanceof AnonymousAuthenticationToken)) {
            Object principal = authentication.getPrincipal();
            if (principal instanceof UserDetails) {
                LoginUser loginUser = (LoginUser) principal;
                // 获取userId
                Long userId = loginUser.getUserId();
                return userId;
            }
        }
        return null;
    }

    /**
     * 修改comment
     *
     * @param comment comment
     * @return 结果
     */
    @Override
    public int updateComment(Comment comment) {
        return commentMapper.updateComment(comment);
    }

    /**
     * 批量删除comment
     *
     * @param ids 需要删除的comment主键
     * @return 结果
     */
    @Override
    public int deleteCommentByIds(Long[] ids) {
        return commentMapper.deleteCommentByIds(ids);
    }

    /**
     * 删除comment信息
     *
     * @param id comment主键
     * @return 结果
     */
    @Override
    public int deleteCommentById(Long id) {
        return commentMapper.deleteCommentById(id);
    }
}
